<?php

class PhpFox_Mod_Video_ComponentView extends BaseComponent
{
    function process()
    {
    	App::checkUserAccess('video', array('full', 'read_only'));

    	$oSrvVideo = &$this->_oModule->getService('Video');

    	$oReq = &App::getRequest();

    	$txt = App::newObject('Text');

 		$oSrvSec = &App::getModuleService('Account', 'Security');

 		$iViewId = $this->getParam('itemid');

 		if ( !$iViewId || !is_numeric($iViewId) )
 		{
 			App::gotoUrl('public.videos');
 		}

 		$b = $this->oDatabase->getRow("
 			SELECT 
 				vid_id, vid_title, vid_info, vid_time, vid_total, vid_type, vid_rating, vid_url, vid_userid, stream_id, vid_tags, vid_rating_count, featured, comments, list.list_name, list.list_id, user.user 
 			FROM ". App::getT('videos') ." AS vids
				LEFT JOIN ". App::getT('videos_list') ." AS list ON(vids.vid_list_id = list.list_id)
				JOIN ". App::getT('user') ." AS user ON(vids.vid_userid = user.id)
			WHERE vids.vid_id = '$iViewId'
			");

 		if (!isset($b['vid_id'])) 
 		{
 			App::gotoUrl('public.videos');
 		}
 		
		$stream_url = (!empty($b['vid_url']) ? $oSrvVideo->getStreamLocation($b['stream_id'], $b['vid_url']) : App::getParam('url') . "file/player/flvplayer.swf?file=" . App::getParam('video.url') . $b['vid_id'] .'.'. $b['vid_type'] .'');
		if ( $b['vid_type'] == 'flv' || file_exists(App::getParam('video.path') . $b['vid_id'] . '.flv') )
		{
			if ($b['vid_type'] != 'flv' && file_exists(App::getParam('video.path') . $b['vid_id'] . '.flv'))
			{
				$stream_url = str_replace($b['vid_type'], 'flv', $stream_url);
			}
			$sVariables = (App::getParam('ffmpeg_watermark') ? "&logo=". App::getParam('ffmpeg_waterimage', "", App::getParam('url') . "file/pic/watermark/video.gif") ."" : '');
			$sVariables .= "&autostart=". (App::getParam('vp_autostart') ? 'true' : 'false') ."";
			$sVariables .= "&repeat=". (App::getParam('vp_repeat') ? 'true' : 'false') ."";
			$sVariables .= "&backcolor=". App::getParam('vp_backcolor') ."";
			$sVariables .= "&frontcolor=". App::getParam('vp_frontcolor') ."";
			$sVariables .= "&lightcolor=". App::getParam('vp_lightcolor') ."";
			$sVariables .= "&link=". App::makeUrl('public.videos') ."";
			$sVariables .= "&allowfullscreen=true";
  			$stream_url = str_replace(App::getParam('video.url'), '', $stream_url) . $sVariables;
		}

  		$b['vid_total']++;
  		$b['vid_info'] = $txt->parseText($b['vid_info']);
		$b['url'] = App::makeUrl('public.videos.id_'. $b['vid_id'] .'');
		$b['embed'] = htmlspecialchars('<object width="425" height="350"><param name="movie" value="'. $stream_url .'"></param><param name="wmode" value="transparent"></param><embed src="'. $stream_url .'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>');

		if (CAN_SET_COOKIE && !isset($_SESSION['view_video' . $b['vid_id']]))
		{
			$this->oDatabase->query("UPDATE ". App::getT('videos') ." SET vid_total = '$b[vid_total]' WHERE vid_id = '$b[vid_id]'");
			$_SESSION['view_video' . $b['vid_id']] = true;
		}

		$i = 0;
		$aWarnings = array();
		foreach(App::getMessages('Mod_Video.warning') as $aWarning)
		{
			$i++;
			$aWarnings[$i] = $aWarning;
		}
		App::setAccessForContent('video', $iViewId);
		$this->oTpl->assignSrc(array
		(
			'vidInfo' => $b['vid_info'],
		));
		$this->oTpl->assign(array
		(
			'bIsStaff' => App::isStaff(),
			'vidUrl' => $oSrvVideo->displayVideo($b),
			'b' => $b,
			'bLogged' => App::getUser('id'),
			'sUImageUrl' => App::getParam('user.pic.url'),
			'xajax' => App::getParam('useAjax'),
			'iVidRating' => $b['vid_rating'],
			'aWarnings' => $aWarnings,
			'aAjaxArray' => $b['vid_id'],
		));
		return $this->oTpl->fetch('View.html');
	}
}

?>
